#include "stdafx.h"
#include "timer.h"

unsigned char TmrCount = 0;
unsigned char T0RH = 0;//T0重载值的高字节
unsigned char T0RL = 0;//T0重载值的低字节

/* 配置并启动T0，ms-T0定时时间 */
void ConfigTimer0(unsigned int ms){
	unsigned long tmp;//临时变量
	tmp = 11059200 / 12;//定时器计数频率
	tmp = (tmp * ms) / 1000;//计算所需的计数值
	tmp = 65536 - tmp;//计算定时器重载值
	tmp = tmp + 13;//补偿中断响应延时造成的误差
	T0RH = (unsigned char)(tmp>>8);//定时器重载值拆分为高低字节
	T0RL = (unsigned char)tmp;
	TMOD &= 0xF0;//清零T0的控制位
	TMOD |= 0x01;//配置T0为模式1
	TH0 = T0RH;//加载T0重载值
	TL0 = T0RL;
	ET0 = 1;//使能T0中断
	TR0 = 1;//启动T0
}

/* T0中断服务函数，完成计数 */
void InterruptTimer0() interrupt 1{
	static unsigned char tmr = 0;

	TH0 = T0RH;//重新加载重载值
	TL0 = T0RL;
	//定时1s
	if(tmr >= 1000){
		tmr = 0;
		TmrCount++;
	}else{
		tmr++;
	}
}

void ResetTmr(void){
	TH0 = T0RH;//重新加载重载值
	TL0 = T0RL;
}

/* 秒延时 *//*
void delayNs(unsigned char s){
	count = s;
	TH0 = T0RH;//重新加载重载值
	TL0 = T0RL;
	while(count);
}
*/
